package com.kjsdfgk.hjmdsf.ftmine.mvp.ui.fragment;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.TimeUtils;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import com.kjsdfgk.hjmdsf.app.entity.Account;
import com.kjsdfgk.hjmdsf.app.utils.SharedPrefsUtils;
import com.kjsdfgk.hjmdsf.charge.mvp.ui.activity.MemberChargeActivity;
import com.kjsdfgk.hjmdsf.favorite.mvp.ui.activity.FavoriteActivity;
import com.kjsdfgk.hjmdsf.ftmine.di.component.DaggerMineV3FtComponent;
import com.kjsdfgk.hjmdsf.ftmine.mvp.contract.MineV3FtContract;
import com.kjsdfgk.hjmdsf.ftmine.mvp.presenter.MineV3FtPresenter;
import com.kjsdfgk.hjmdsf.login.mvp.ui.activity.LoginActivity;
import com.kjsdfgk.hjmdsf.history.mvp.ui.activity.HistoryActivity;
import com.kjsdfgk.hjmdsf.register.mvp.ui.activity.RegisterActivity;
import com.lksdkjs.live.R;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView;
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView;


import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 04/30/2020 17:57
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class MineV3FtFragment extends BaseFragment<MineV3FtPresenter> implements MineV3FtContract.View , View.OnClickListener {

    public static MineV3FtFragment newInstance() {
        MineV3FtFragment fragment = new MineV3FtFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerMineV3FtComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }
    @BindView(R.id.mine_list)
    QMUIGroupListView mineList;
    @BindView(R.id.btn_logout)
    Button btnLogout;
    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mine_v3_ft, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        QMUICommonListItemView itemView1 = mineList.createItemView("续费充值");
        itemView1.setAccessoryType(QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON);

        QMUICommonListItemView itemView2 = mineList.createItemView("我的消息");
        itemView2.setAccessoryType(QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON);

        QMUICommonListItemView itemView3 = mineList.createItemView("我的积分");
        itemView3.setAccessoryType(QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON);

        QMUICommonListItemView itemView4 = mineList.createItemView("我的收藏");
        itemView4.setAccessoryType(QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON);

        QMUICommonListItemView itemView31 = mineList.createItemView("浏览历史");
        itemView31.setAccessoryType(QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON);



        QMUICommonListItemView itemView5 = mineList.createItemView("清空缓存");
        itemView5.setAccessoryType(QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON);

        QMUICommonListItemView itemView6 = mineList.createItemView("联系客服");
        itemView6.setAccessoryType(QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON);

        QMUICommonListItemView itemView7 = mineList.createItemView("分享应用");
        itemView6.setAccessoryType(QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON);

        QMUICommonListItemView itemView8 = mineList.createItemView("版本更新");
        itemView7.setAccessoryType(QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON);

        QMUIGroupListView.newSection(getActivity())
                .addItemView(itemView1, this)
                .addItemView(itemView2, this)
                .addItemView(itemView3, this)
                .addItemView(itemView4, this)
                .addItemView(itemView31, this)
                .addItemView(itemView5, this)
                .addItemView(itemView6, this)
                .addItemView(itemView7, this)
                .addItemView(itemView8, this)
                .addTo(mineList);

        btnLogout.setOnClickListener(this);
        headerView.setOnClickListener(this);

        mPresenter.checkLogin();
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading(String key) {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public boolean isCancel() {
        return false;
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    @Override
    public void onClick(View v) {
        if (v instanceof QMUICommonListItemView) {
            String string = String.valueOf(((QMUICommonListItemView) v).getText());
            actionClickList(string);
        } else {
            switch (v.getId()) {
                case R.id.navigation_member_name:
                    if ( mPresenter.getAccount() == null){
                        showLoginDlg();
                    }
                    break;
                case R.id.btn_logout:
                    mPresenter.logOut();
                    break;
               case R.id.header_view:
                   if ( mPresenter.getAccount() == null){
                       showLoginDlg();
                   }
                    break;
                default:
                    break;
            }
        }
    }




    private void actionClickList(String s) {

        switch (s){
            case "续费充值":
                Account account = mPresenter.getAccount();
                if (account!=null){
                    Intent intent = new Intent(getActivity() , MemberChargeActivity.class);
                    launchActivity(intent);
                }else {
                    Intent intent = new Intent(getActivity() , LoginActivity.class);
                    launchActivity(intent);
                }
                break;
            case "我的消息":
                break;
            case "我的积分":
                break;
            case "我的收藏":
                if ( mPresenter.getAccount() == null){
                    showLoginDlg();
                }else {
                    startActivity(new Intent(getActivity() , FavoriteActivity.class));
                }
                break;
            case "浏览历史":
                if ( mPresenter.getAccount() == null){
                    showLoginDlg();
                }else {
                    startActivity(new Intent(getActivity() , HistoryActivity.class));
                }
                break;

            case "清空缓存":
                break;
            case "分享应用":
                // 获取系统剪贴板
                if ( mPresenter.getAccount() == null){
                    showLoginDlg();
                }else {
                    ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);

// 创建一个剪贴数据集，包含一个普通文本数据条目（需要复制的数据）
                    ClipData clipData = ClipData.newPlainText(null, "我发现了一个好玩的应用，"+ SharedPrefsUtils.getString("appUrl","") +"，填写我的邀请ID（"+mPresenter.getAccount().getId()+"）送会员");

// 把数据集设置（复制）到剪贴板
                    clipboard.setPrimaryClip(clipData);
                    showMessage("信息已复制 ，一般人别分享出去");
                }
                break;
            case "版本更新":
                showMessage("当前已为最新版本");
                break;
        }

    }

    private void showLoginDlg() {
        QMUIDialog.MessageDialogBuilder builder = new QMUIDialog.MessageDialogBuilder(getActivity());
        builder.setTitle("系统提示");
        builder.setMessage("您尚未登录");

        builder.addAction("马上注册", (dialog, index) -> {
            dialog.cancel();
            Intent intent = new Intent(getActivity() , RegisterActivity.class);
            startActivity(intent);
        });
        builder.addAction("立即登录", (dialog, index) -> {
            dialog.cancel();
            Intent intent = new Intent(getActivity() , LoginActivity.class);
            startActivity(intent);

        });


        builder.show();
    }


    @BindView(R.id.header_view)
    View headerView;
    @BindView(R.id.navigation_member_name)
    TextView memberName;
    @BindView(R.id.navigation_expire_time)
    TextView expireTime;

    @BindView(R.id.navigation_invite_code)
    TextView invoteCode;
    @Override
    public void showInitView() {
        memberName.setText("请登陆" );
        expireTime.setText("----" );
        invoteCode.setText("----");
        btnLogout.setVisibility(View.INVISIBLE);
    }

    @Override
    public void showAccount(Account user) {
        memberName.setText("用    户："+user.getName());
        expireTime.setText("到    期："+ TimeUtils.millis2String(Long.parseLong(user.getVip())*1000) );
        invoteCode.setText("邀请ID："+  user.getId());
        btnLogout.setVisibility(View.VISIBLE);
    }
}
